package main

import (
	"DCOS-API/api"
	"DCOS-API/config"
	"DCOS-API/middle"
	"context"
	"fmt"
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	engine := gin.Default()
	engine.Use(middle.Cors())
	api.Init(engine)
	conf := config.GetConfig()
	baseConf := conf.BaseConfig
	srv := &http.Server{
		Addr:    fmt.Sprintf(":%v", baseConf.PORT),
		Handler: engine,
	}
	fmt.Printf("host: %v:%v\n", "http://127.0.0.1", baseConf.PORT)
	//注册api服务
	go func() {
		// service connections
		if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
			log.Fatalf("listen: %s\n", err)
		}
	}()

	quit := make(chan os.Signal)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
	<-quit
	log.Println("Shutdown Server ...")

	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
	defer cancel()
	if err := srv.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}
	select {
	case <-ctx.Done():
		log.Println("timeout of one seconds.")
	}
	log.Println("Server exiting")
}

